import Vue from 'vue'
import Router from 'vue-router'
// import store from '@/store/index'
Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  }
]

// 导入modules的动态路由
import permissions from './modules/permission'
import rolelist from './modules/rolelist'
import employees from './modules/employees'
import property from './modules/property'
import category from './modules/category'
import manager from './modules/manager'
import client from './modules/client'
import pages from './modules/pages'
// 设置动态路由
export const asyncRoutes = [permissions, employees, rolelist, property, category, manager, client, pages]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // 解构动态路由和静态路由
  // routes: [...constantRoutes, ...asyncRoutes]
  routes: constantRoutes
})

const router = createRouter()
// router.beforeEach(async(to, from, next) => {
//   const token = localStorage.getItem('house-token-8')
//   console.log('进入守卫')
//   const whiteArr = ['/404', '/login']
//   // 如果没有token,并且去往白名单内的页面,放行,否则跳转到登录页面
//   if (!token) {
//     console.log('没有token')
//     if (whiteArr.indexOf(to.path) !== -1) {
//       next()
//     } else {
//       next('/login')
//     }
//   } else { // 有token,表示已登录,如果去往登录页面,跳转到首页
//     console.log('有token')
//     if (to.path === '/login' && token) {
//       console.log('进入')
//       next('/')
//     } else {
//       console.log('进入2')
//       if (!store.state.userInfo) {
//         await store.dispatch('user/updateUserinfo')
//         console.log('456')
//       }
//       next()
//     }
//   }
// })
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
